package com.cainiaoshixi.admin.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cainiaoshixi.admin.common.Result;
import com.cainiaoshixi.admin.entity.CompanyEmails;
import com.cainiaoshixi.admin.entity.Job;
import com.cainiaoshixi.admin.entity.JobWithBLOBs;
import com.cainiaoshixi.admin.service.IJobService;
import com.cainiaoshixi.admin.utils.mail.SendMailTest;
import com.cainiaoshixi.admin.vo.JobQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @Author: Chy
 * @Description:
 * @Date: Created at 21:51 2018/1/29
 */
@Controller
@RequestMapping("/jobs")
public class JobController {
    @Autowired
    private IJobService jobService;

    /**
     * @Author: Chy
     * @Param:
     * @Description: index页跳转
     * @Date: 21:52 2018/1/29
     */
    @RequestMapping(value = {"", "/index"})
    public String index() {
        return "jobList";
    }

    /**
     * @Author: Chy
     * @Param:
     * @Description 条件查询
     * @Date: Created in 13:47 2018/1/31
     */
    @ResponseBody
    @RequestMapping("/jobList")
    public String getJobList(JobQueryVo jobQueryVo) {

        JSONObject jsonObject = new JSONObject();
        jobQueryVo = parseJson2Vo(jobQueryVo);
        int totalJobCount = jobService.getJobCountByBo(jobQueryVo);  //查询总数
        List<JobWithBLOBs> jobList = jobService.getJobListByVo(jobQueryVo);  //条件查询

        jsonObject.put("sEcho", jobQueryVo.getsEcho());
        jsonObject.put("iTotalRecords", totalJobCount);
        jsonObject.put("iTotalDisplayRecords", totalJobCount);
        jsonObject.put("aData", jobList);
        return jsonObject.toJSONString();
    }

    /**
    * @Author: Chy
    * @Param:
    * @Description 查看岗位详情
    * @Date: Created in 14:28 2018/2/1
    */
    @ResponseBody
    @RequestMapping("/jobDetail/{id}")
    public JobWithBLOBs getJobDetail(Model model, @PathVariable Long id){
        JobWithBLOBs jobWithBLOBs = jobService.selectByPrimaryKey(id);
        return jobWithBLOBs;
    }

    /**
     * @Author: Chy
     * @Param:
     * @Description: 更新岗位详情
     * @Date: 12:49 2018/2/6
     */
    @ResponseBody
    @RequestMapping("/updateJob")
    public String updateJob(@RequestBody JobWithBLOBs jobWithBLOBs) {
        jobWithBLOBs.setUpdatedTime(new Date());
        if (jobWithBLOBs.getPublished() == null){
            jobWithBLOBs.setPublished((byte) 0);
        }
        jobService.updateById(jobWithBLOBs);
        return "{\"success\": true}";
    }

    /**
     * @Author: Chy
     * @Param:
     * @Description: 添加岗位跳转
     * @Date: 21:33 2018/2/6
     */
    @RequestMapping("/addJob")
    public String addJob(){
        return "addJob";
    }

    /**
     * @Author: Chy
     * @Param:
     * @Description: 添加岗位
     * @Date: 22:28 2018/2/6
     */
    @ResponseBody
    @RequestMapping("/insertJob")
    public String insertJob(@RequestBody JobWithBLOBs jobWithBLOBs) {
        CompanyEmails companyEmails = new CompanyEmails();
        jobWithBLOBs.setCreatedTime(new Date());
        jobWithBLOBs.setUpdatedTime(new Date());
        if (jobWithBLOBs.getPublished() == null){
            jobWithBLOBs.setPublished((byte) 0);
        }
        jobService.insertJob(jobWithBLOBs);


        companyEmails.setEmail(jobWithBLOBs.getEmail());
        companyEmails.setSendTime(jobWithBLOBs.getCreatedTime());
        companyEmails.setSendHandler (jobWithBLOBs.getSendHandler());
        int totalEmailCount = jobService.getEmailCountByEmail(companyEmails);  //查询总数
        if(totalEmailCount == 0) {
            jobService.insertEmail(companyEmails);
            boolean mailSendResult=SendMailTest.send(new String[]{companyEmails.getEmail()});
            if (mailSendResult == false){
                return "{\"success\": true,\"mailsuccess\": false}";
            }
            return "{\"success\": true,\"mailsuccess\": true}";
        }
        return "{\"success\": true}";
    }

    /**
     * @Author: Chy
     * @Param:
     * @Description 解析json串中的内容并封装到queryVo对象中
     * @Date: Created in 16:17 2018/1/31
     */
    private JobQueryVo parseJson2Vo(JobQueryVo jobQueryVo) {
        String aoData = jobQueryVo.getAoData();
        JSONArray jsonArray = JSONArray.parseArray(aoData);
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObj = (JSONObject) jsonArray.get(i);
            if (jsonObj.get("name").equals("sEcho")){
                jobQueryVo.setsEcho(jsonObj.getIntValue("value"));
            }else if (jsonObj.get("name").equals("iDisplayStart")){
                jobQueryVo.setiDisplayStart(jsonObj.getIntValue("value"));
            }else if(jsonObj.get("name").equals("iDisplayLength")){
                jobQueryVo.setiDisplayLength(jsonObj.getIntValue("value"));
            }else if(jsonObj.get("name").equals("job")){
                jobQueryVo.setJob(jsonObj.getString("value"));
            }else if(jsonObj.get("name").equals("type")){
                jobQueryVo.setType(jsonObj.getString("value"));
            }else if(jsonObj.get("name").equals("published")){
                jobQueryVo.setPublished(jsonObj.getByteValue("value"));
            }
        }
        return jobQueryVo;
    }
}
